﻿using FairyGUI;
using System;
using System.Collections;
using UnityEngine;

/// <summary>
/// 登录流程
/// </summary>
public class LoginProcedure : BaseProceduar {

    UILoginWindow _ui;

    static LoginProcedure _inst;

    public static LoginProcedure inst {
        get {
            if (_inst == null)
            {
                _inst = new LoginProcedure();
            }
            return _inst;
        }
    }

    public override void EnterProceduar(bool initUI)
    {
        DebugInfo.Log("进入登录流程");

        // 初始化
        LocalStorage.ClearKey("弹出公告");


        // 播放音效
        AudioController.inst.PlayBGM(AudioController.LoadClip("主背景音乐"));

        // 打开连接
        if (!NetIOController.inst.ready)
        {
            NetIOController.inst.Connect(Constants.LOGIN_URL);
        }

        if (initUI)
        {
            InitUI();
        }

        // 其他平台缓存登录信息，不需要每次都登录
#if !UNITY_STANDALONE_WIN
        if (LoginReqInfo.HasCache())
        {
            LoginReqInfo reqInfo = LoginReqInfo.FromCache();
            if ((DateHelper.timestamp < reqInfo.timeout) 
                && reqInfo.valid)
            {
                DataKernel.inst.loginReqInfo = reqInfo;
            }
        }
#endif
    }

    public override void UpdateProceduar()
    {
        base.UpdateProceduar();
    }

    public override void LeaveProceduar()
    {
        DebugInfo.Log("退出登录流程");
    }

    protected override void HandleMessage(Message message)
    {
        switch(message.msgId)
        {
            case Commands.CMD_CTRL_BUTTON_CLICKED:
                switch(message.iParam)
                {
                    case GameCommands.CMD_CTRL_LOGIN:
                        HandleLoginCmd();
                        break;
                }
                break;

            default:
                base.HandleMessage(message);
                break;
        }
    }

    protected override bool ProcessPacket(IOMessage message)
    {
        switch (message.code)
        {
            case IOCmds.WS_CONN_OPEN:
                // 与服务器的连接打开
                UIRootController.inst.ShowMask(false);
                if (DataKernel.inst.loginReqInfo != null)
                {
                    DoLogin(DataKernel.inst.loginReqInfo);
                }
                return true;

            default:
                return base.ProcessPacket(message);
        }
    }

    void InitUI()
    {
        _ui = new UILoginWindow();
        UIRootController.inst.SwitchTo(_ui);
        
//#if UNITY_STANDALONE_WIN // TODO 打开本注释已在正常情况下隐藏平台编号输入控件
//        _ui.hiddenOpenidInput();        
//#endif

        DebugInfo.Log("登陆界面初始化完毕");
    }

    void HandleLoginCmd()
    {
        DebugInfo.Log("开始登录");

        if (!_ui.agreementOk)
        {
            DebugInfo.Error("请同意用户协议");
            return;
        }

#if UNITY_STANDALONE_WIN
        LoginReqInfo reqInfo = new LoginReqInfo();
        reqInfo.openid = _ui.openid;
        reqInfo.name = _ui.openid;
        reqInfo.headpic = "";
        reqInfo.sex = 0;
        DoLogin(reqInfo);
#else
        WxManager.inst.Login(reqInfo => {
            // TODO 删除下面给登陆请求字段复制的代码，正常情况下这些字段已经正确复制
            reqInfo.openid = _ui.openid;
            reqInfo.name = _ui.openid;
            reqInfo.headpic = "";
            reqInfo.sex = 0;
            reqInfo.timeout = Constants.LOGIN_TIMEOUT + DateHelper.timestamp; // 有效期1天

            DoLogin(reqInfo);
        });
#endif
    }

    void DoLogin(LoginReqInfo reqInfo)
    {
        if (!reqInfo.valid)
        {
            DebugInfo.Error("登录信息无效");
            return;
        }

        // 保存数据
#if !UNITY_STANDALONE_WIN
        reqInfo.SetCache();
#endif

        DataKernel.inst.loginReqInfo = reqInfo;
        // 切换到主界面流程
        GameController.inst.SwitchTo(HomeProcedure.inst);
    }
}
